//axios 简单配置
//引入下载后的axios
import router from "@/router"
import axios from "axios"

//引入elementui
// import ElementUI from "element-ui"
// console.log(ElementUI);
//解构引入MessageBox Message
import { MessageBox, Message } from "element-ui"
// console.log(MessageBox);
// console.log(Message);

//axios基础配置
let http = axios.create({
    withCredentials: true,//是否携带凭证
    baseURL: "/api",//基础地址
    // timeout: 5000//超时设置
})

//引入nprogress
import nprogress from "nprogress"
//请求拦截
http.interceptors.request.use(config => {
    //打开进度条
    nprogress.start()
    //config 就是请求的对象，里面可以拿到请求的地址，参数等
    //console.log(config);
    let token = localStorage.getItem("yz-token")
    config.headers.authorization = token
    //return config 理解为放行
    return config
})
//响应拦截
http.interceptors.response.use(config => {
    //关闭进度条
    nprogress.done()
    //config 就是响应的对象，可以拿到服务器响应的内容

    //console.log(config);

    let { data: { state, msg, code } } = config
    // console.log(state);
    // console.log(msg);
    //console.log(code);
    //10022登录过期
    //1004token过期
    //10026没有权限
    let codes = ["10022", "1004", "10026"]
    if (codes.includes(code)) {
        //清除token
        localStorage.removeItem("yz-token")
        //刷新页面
        location.reload()
    }

    //一个需要排除的不弹窗的接口地址
    let excludeUrl = ["/order/query_order_status","/students/updatestu"]
    let isExclude = excludeUrl.includes(config.config.url)
    //登录失败弹出提示框
    if (!state && !isExclude) {
        MessageBox.alert(msg, '提示', { type: "error" })
    }
    //return config 可以理解为放行
    return config
})

export default http